package com.xli.sso.security;

import com.xli.organ.user.frameuser.entity.FrameUser;
import com.xli.organ.user.frameuser.service.IFrameUserService;
import com.xli.sso.security.identity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private IFrameUserService iFrameUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询
        FrameUser frameUser = iFrameUserService.findUserByLoginId(username);
        if (frameUser == null) {
            throw new UsernameNotFoundException("User not found -> 用户不存在：" + username);
        }

        return new LoginUser(frameUser.getLogin_id(), frameUser);
    }
}
